<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="工单编码" prop="orderCode">
        <el-input v-model="queryParams.orderCode" placeholder="请输入工单编码" clearable size="small" style="width: 200px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总成条码" prop="assemblyBarcode">
        <el-input v-model="queryParams.assemblyBarcode" placeholder="请输入总成条码" clearable size="small"
          style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status" label-width="60px">
        <el-select v-model="queryParams.status" placeholder="请选择状态" style="width: 200px"
          @keyup.enter.native="handleQuery">
          <el-option v-for="item in mesTypeOptions" :key="item.dictValue" :label="item.dictLabel"
            :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['function:order:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['function:order:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['function:order:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['function:order:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="arrangeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="工单主键id" align="center" prop="id" v-if="false" />
      <el-table-column label="工单编码" align="center" prop="orderCode" />
      <el-table-column label="序列号" align="center" prop="serialNumber" />
      <el-table-column label="顺序号" align="center" prop="sequence" />
      <el-table-column label="总成条码" align="center" prop="assemblyBarcode" />
      <el-table-column label="总成型号" align="center" prop="productCode" />
      <el-table-column label="状态" align="center" prop="status" :formatter="mesTypeModeFormat" />
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="操作时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['function:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['function:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改生产队列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="工单编码" prop="orderCode">
              <el-input v-model="form.orderCode" placeholder="请输入工单编码" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="物料名称" prop="materialCode">
              <el-select v-model="form.materialCode" placeholder="请选择物料名称" style="width: 240px">
                <el-option v-for="item in materialOptions" :key="item.materialCode" :label="item.materialName"
                  :value="item.materialCode">
                  <span style="float: left">{{ item.materialCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.materialName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="产线名称" prop="lineCode">
              <el-select v-model="form.lineCode" placeholder="请选产线名称" style="width: 240px">
                <el-option v-for="item in lineOptions" :key="item.shopCode" :label="item.shopName"
                  :value="item.shopCode">
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="产品名称" prop="productCode">
              <el-select v-model="form.productCode" placeholder="请选产品名称" style="width: 240px">
                <el-option v-for="item in productOptions" :key="item.modelCode" :label="item.ledName"
                  :value="item.modelCode">
                  <span style="float: left">{{ item.modelCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.ledName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工艺路线名称" prop="routingCode">
              <el-select v-model="form.routingCode" placeholder="请选择工艺路线名称" style="width: 240px"
                @change="routingCodeChange">
                <el-option v-for="item in routingOptions" :key="item.id" :label="item.operationName"
                  :value="item.routingCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="计划编码" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请输入计划号" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工单数量" prop="orderNumber">
              <el-input v-model="form.orderNumber" placeholder="请输入工单数量" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="班组" prop="team">
              <el-input v-model="form.team" placeholder="请输入班组" style="width: 240px" />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" style="width: 240px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listArrange,
    getOrder,
    delOrder,
    transmitOrder,
    addOrder,
    updateOrder,
    exportOrder
  } from "@/api/function/arrange";
  import {
    listMaterial,
    listProduct,
    listOperations
  } from "@/api/function/prod";
  import { getTree } from "@/api/pull/feedSet";

  export default {
    name: "Order",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 生产队列表格数据
        arrangeList: [],
        // 生产队列状态
        mesTypeOptions: [],
        // 物料名称下拉框
        materialOptions: [],
        // 产线名称下拉框
        lineOptions: [],
        // 产品名称下拉框
        productOptions: [],
        // 工艺路线名称下拉框
        routingOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderCode: undefined,
          assemblyBarcode: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          orderCode: [{ required: true, message: "工单编码不能为空", trigger: "change" }],
          materialCode: [{ required: true, message: "物料编码不能为空", trigger: "change" }]
        }
      };
    },
    created() {
      this.getMaterialList();
      this.getLineList();
      this.getProductList();
      this.getRoutingList();
      this.getList();

      this.getDicts("mes_type").then((response) => {
        this.mesTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询生产队列列表 */
      getList() {
        this.loading = true;
        listArrange(this.queryParams).then(response => {
          this.arrangeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      routingCodeChange(val) {
        this.$forceUpdate()
      },
      // 工单状态字典翻译
      mesTypeModeFormat(row, column) {
        return this.selectDictLabel(this.mesTypeOptions, row.status);
      },
      // 查询物料下拉
      getMaterialList() {
        listMaterial({}).then(response => {
          this.materialOptions = response;
        });
      },
      // 查询产线下拉
      getLineList() {
        getTree(3).then(response => {
          this.lineOptions = response.data;
        });
      },
      // 查询产品下拉
      getProductList() {
        listProduct({}).then(response => {
          this.productOptions = response.data;
        });
      },
      // 查询工艺路线下拉
      getRoutingList() {
        listOperations({}).then(response => {
          this.routingOptions = response;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.orderId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加工单信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const orderId = row.orderId || this.ids;
        getOrder(orderId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改工单信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.orderId != null) {
              updateOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addOrder(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const orderIds = row.orderId || this.ids;
        this.$confirm('是否确认删除工单信息编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () { });
      },
      /** 下发按钮操作 */
      handleTransmit(row) {
        const orderIds = row.orderId || 0;
        this.$confirm('是否确认下发工单信息编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return transmitOrder(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("下发成功");
        }).catch(function () { });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有工单信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () { });
      }
    }
  };
</script>